package zhengdaoyang.settings.dao;

import zhengdaoyang.settings.domain.Cart;
import zhengdaoyang.settings.domain.Goods;
import zhengdaoyang.settings.domain.User;
import zhengdaoyang.utils.MyDbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao{
    @Override
    public List<Cart> findallcart() {
        List<Cart> cartList = new ArrayList<Cart>();
        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection="+connection);

        /*预编译*/
        String sql = "select * from cart ";
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs= null;
        String na =null;
        try {
            rs =  ps.executeQuery();
            while(rs.next()){
                Cart cart = new Cart();
                int  bnumber = rs.getInt("bnumber");
                double bprice = rs.getDouble("bprice");
                String bname = rs.getString("bname");
                cart.setBnumber(bnumber);
                cart.setBprice(bprice);
                cart.setBname(bname);
                cartList.add(cart);
            }
        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }finally {
            MyDbUtil.close(connection,ps, rs);
        }
        System.out.println("-------dao层执行完毕="+cartList);

        return cartList;
    }

    //更新购物车商品数量
    @Override
    public int updateCart(int num,String name) {

        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection="+connection);
        User user = new User();
        /*预编译*/
        String sql = "update cart set bnumber=? where bname=? ";

        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setInt(1,num);
            ps.setString(2,name);
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs= null;
        String na =null;
        int kk = 0;
        try {
          kk=   ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }finally {
            MyDbUtil.close(connection,ps, rs);
        }
        System.out.println("-------dao层执行完毕=");

        return kk;
    }

    @Override
    public int deleteCartGoods(String name) {
        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection="+connection);
        User user = new User();
        /*预编译*/
        String sql = "delete from cart where bname=?";

        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,name);
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs= null;
        String na =null;
        int kk = 0;
        try {
            kk=   ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }finally {
            MyDbUtil.close(connection,ps, rs);
        }
        System.out.println("-------dao层执行完毕=");
        return kk;
    }

    @Override
    public int deleteAllCartGoods() {
        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection="+connection);
        User user = new User();
        /*预编译*/
        String sql = " TRUNCATE TABLE cart";

        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs= null;
        String na =null;
        int kk = 0;
        try {
            kk=   ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }finally {
            MyDbUtil.close(connection,ps, rs);
        }
        System.out.println("-------dao层执行完毕=");
        return kk;
    }

    @Override
    public int insertorders(int uid,String address,String phone,String username,Double omoney) {

        Connection connection = MyDbUtil.getConnection();

        System.out.println("conection="+connection);
        User user = new User();
        /*预编译*/
        String sql = "INSERT into orders(uid,address,phone,username,otime,omoney) VALUES(?,?,?,?,now(),?)";

        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setInt(1,uid);
            ps.setString(2,address);
            ps.setString(3,phone);
            ps.setString(4,username);
            ps.setDouble(5,omoney);
        } catch (SQLException e) {
            System.out.println("创建语句错误");
        }

        ResultSet rs= null;
        String na =null;
        int kk = 0;
        try {
            kk=   ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("执行语句，获取结果集异常");
        }finally {
            MyDbUtil.close(connection,ps, rs);
        }
        System.out.println("-------dao层执行完毕=");
        return kk;
    }
}
